package org.ttitfly.ttcore.displaytag;

import org.apache.commons.beanutils.PropertyUtils;
import org.displaytag.decorator.TableDecorator;

public class ItemLineForDispatchDecorator extends TableDecorator {

    private Long getObjId() {
        Object rowObject = this.getCurrentRowObject();
        Long id;
        try {
            id = (Long) PropertyUtils.getProperty(rowObject, "id");
        } catch (Exception e) {
            id = new Long(-1);
        }
        return id;
    }

    private String getObjCarrierName() {
        Object rowObject = this.getCurrentRowObject();
        String name = "";
        try {
            Object carrier = PropertyUtils.getProperty(rowObject, "carrier");
            name = (String) PropertyUtils.getProperty(carrier, "name");
        } catch (Exception e) {
            name = "";
        }
        return name;
    }

    private String getObjCustomerName() {
        Object rowObject = this.getCurrentRowObject();
        String name = "";
        try {
            Object carrier = PropertyUtils.getProperty(rowObject, "customer");
            name = (String) PropertyUtils.getProperty(carrier, "name");
        } catch (Exception e) {
            name = "";
        }
        return name;
    }

    private boolean getObjCarrierIsCalculated() {
        Object rowObject = this.getCurrentRowObject();
        boolean caiculated;
        try {
            caiculated = ((Boolean) PropertyUtils.getProperty(rowObject, "chargeSaved")).booleanValue();
        } catch (Exception e) {
            caiculated = false;
        }
        return caiculated;
    }

    private boolean getObjCustomerIsCalculated() {
        Object rowObject = this.getCurrentRowObject();
        boolean caiculated;
        try {
            caiculated = ((Boolean) PropertyUtils.getProperty(rowObject, "chargeSaved")).booleanValue();
        } catch (Exception e) {
            caiculated = false;
        }
        return caiculated;
    }

    public String getCarrierRow() {
        Long id = getObjId();
        String carrierName = getObjCarrierName();
        if (getObjCarrierIsCalculated()) {
            return "<input type='checkbox' name='checkboxList' value='" + id + "' onclick=\"checkCarrier(" + id + ",'"
                    + carrierName + "',this)\" id='T" + id + "'/>";
        } else {
            return "<input type='checkbox' name='checkboxList' value='" + id + "' onclick=\"checkCarrier(" + id + ",'"
                    + carrierName + "',this)\" disabled=\"disabled\" id='T" + id + "'/>";
        }

    }

    public String getCustomerRow() {
        Long id = getObjId();
        String customerName = getObjCustomerName();
        if (getObjCustomerIsCalculated()) {
            return "<input type='checkbox' name='checkboxList' value='" + id + "' onclick=\"checkCustomer(" + id + ",'"
                    + customerName + "',this)\" id='S" + id + "'/>";
        } else {
            return "<input type='checkbox' name='checkboxList' value='" + id + "' onclick=\"checkCustomer(" + id + ",'"
                    + customerName + "',this)\" disabled=\"disabled\" id='S" + id + "'/>";
        }

    }

    @Override
    public String addRowId() {
        Long id = getObjId();
        String rowNumber = id.toString();

        return rowNumber + "\" onclick=\"true\" ondblclick=\"true";
    }

}
